Expressions and operators
Expressions
Expressions is a unit of code that can be evaluated to a value.
var x = 3 + 1;
The above given is an assignment statements which takes expression 3 + 1 and puts its value in variable x. Literals, variables and simple expressions are used with operator for complex expressions.
Operators
Types
The types of operators in JavaScript are:
- Arithemetic Operators: Includes addition, subtraction, multiplication, division, exponentiation, modulus, increment and decrement.
- Assignment Operators: Includes =, +=, -=, *=, /=, %=, **= operations.
let x = 10;
let y = x;
console.log(y); // Expected value: 10
x += 1; // same as x = x + 1
console.log(x); // Expected value: 11
x -= 2; // same as x = x - 2
console.log(x); // Expected value: 9
x *= 10; // same as x = x * 10
console.log(x); // Expected value: 90
x /= 9; // same as x = x / 9
console.log(x); // Expected value: 10
x %= 3; // same as x = x % 3
console.log(x); // Expected value: 1
y **= 2; // same as y = y ** 2
console.log(y); // Expected value: 100
- String Operators: The + can be used to concatenate strings.
- Comparison Operators: Includes ==, ===, !=, >, <, >=, <=, ? operations.
- Logical Operators: Includes &&, ||, ! operations.
var x = 3 * 2;
var x = 5;
var y = 10;
var x == 5;
if((x>>3) && (y>>3)) {
z = x + y;
}